package com.yjd.merchant.app.api.order;

import com.yjd.comm.base.control.MerchantBaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.repayment.dto.OverdueDto;
import com.yjd.comm.repayment.dto.OverdueInfoDto;
import com.yjd.comm.repayment.model.RepaymentDataModel;
import com.yjd.comm.repayment.service.IRepaymentDataService;
import com.yjd.comm.user.dto.ContactInfo;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.model.UserModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.user.service.IUserService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页（我的订单）
 * Created by yjd-wap on 2017/11/28.
 */
@Controller()
@Scope("prototype")
@RequestMapping("/myOrder/")
public class MyOrderControl extends MerchantBaseControl {
    @Resource
    private IOrderService orderService;

    @Resource
    private IUserDataService userDataService;

    @Resource
    private IUserService userService;

    @Resource
    private IRepaymentDataService repaymentDataService;



    /**
     * 首页接口（未读数量）
     * @throws
     */
    @RequestMapping(value = "summary/info")
    public void summaryInfo() throws Exception {
        Long org_id = super.getMerchantId();
        int orderCount = orderService.getModelListCount(FrameUtil.newHashMap("org_id",org_id,"status",
                StagesStatusEnum.MERCHANT_HANDING.getCode()),DbWREnums.READ).intValue();
        int cancelCount = orderService.getModelListCount(FrameUtil.newHashMap("org_id",org_id,"status",
                StagesStatusEnum.MERCHANT_CONFIRM_CANCEL.getCode()),DbWREnums.READ).intValue();
       int overdueCount = repaymentDataService.getModelListCount(FrameUtil.newHashMap("org_id",org_id,"repayment_status",0,
                "estimation_repayment_time_le",FrameUtil.getTime(null)),DbWREnums.READ).intValue();
        this.successData(FrameUtil.newHashMap("orderCount", orderCount,"cancelCount",cancelCount,"overdueCount",overdueCount));
    }

    /**
     *
     * 商户订单对应的逾期列表
     * @throws
     */
    @RequestMapping(value = "overdue/list")
    public void overdueList() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        Long currTime = FrameUtil.getTime();
        paramMap.put("org_id",super.getMerchantId());
        paramMap.put("repayment_status",0);
        paramMap.put("sort"," estimation_repayment_time desc ");
        paramMap.put("estimation_repayment_time_le",currTime);
        paramMap.put("rows", "10");
        PagerModel pagerModel = repaymentDataService.getPageModel(paramMap,DbWREnums.READ);
        //查询已经逾期的还款报表
        List<RepaymentDataModel> repaymentDataModelList = pagerModel.getDataList();
        List<OverdueInfoDto> overdueList = new ArrayList<OverdueInfoDto>();
        if(repaymentDataModelList!=null&&repaymentDataModelList.size()>0){
            List<Long> userIdList = FrameUtil.getBeanFieldValues(repaymentDataModelList,"user_id");
            List<Long> orderIdList = FrameUtil.getBeanFieldValues(repaymentDataModelList,"order_id");

            Map<Long,UserDataModel> userDataListMap = userDataService.getModelMapByIds(userIdList,DbWREnums.READ);
            Map<Long,OrderModel> orderModeListMap = orderService.getModelMapByIds(orderIdList,DbWREnums.READ);
            for (RepaymentDataModel dataModel:repaymentDataModelList){
               OverdueInfoDto overdueInfoDto = new OverdueInfoDto();
               UserDataModel userDataModel = userDataListMap.get(dataModel.getUser_id());
               OrderModel orderModel =  orderModeListMap.get(dataModel.getOrder_id());
               overdueInfoDto.setRepayment_id(dataModel.getId());
               overdueInfoDto.setName(userDataModel==null?"":userDataModel.getId_name());
               overdueInfoDto.setPrice(NumberUtil.formatNumber(dataModel.getRepayment_amount(),NumberUtil.FORMAT_4));
               overdueInfoDto.setCurr_limit(dataModel.getPeriod());
                overdueInfoDto.setLimit(orderModel.getPeriod());
                overdueInfoDto.setOrder_no(orderModel.getOrder_no());
                OverdueDto overdueDto = RepaymentUtils.overdueInterest(dataModel.getRepayment_amount(), dataModel.getEstimation_repayment_time(),currTime);
                overdueInfoDto.setOverdue_day(overdueDto.getOverdueDay());
                overdueInfoDto.setOverdue_price(overdueDto.getOverduePrice());
                overdueInfoDto.setOverdue_day(overdueDto.getOverdueDay());
               overdueList.add(overdueInfoDto);
           }
        }
        this.successData(FrameUtil.newHashMap("overdueList",overdueList ));
    }

        @RequestMapping(value = "overdue/info")
        public void overdueInfo(Long repayment_id) throws Exception {
            if(repayment_id==null){
                FrameUtil.throwBaseException1("参数不能为空！");
            }
            RepaymentDataModel repaymentDataModel = repaymentDataService.getModelOne(FrameUtil.newHashMap("id",repayment_id),DbWREnums.READ);
            if(repaymentDataModel==null){
                FrameUtil.throwBaseException1("还款报表");
            }
            Long currTime = FrameUtil.getTime(null);
            OrderModel orderModel = orderService.getModelById(repaymentDataModel.getOrder_id(), DbWREnums.READ);
            UserModel userModel = userService.getModelOne(FrameUtil.newHashMap("user_id",orderModel.getUser_id()),DbWREnums.READ);
            UserDataModel userDataModel = userDataService.getModelOne(FrameUtil.newHashMap("user_id",orderModel.getUser_id()),DbWREnums.READ);
            OverdueDto overdueDto = RepaymentUtils.overdueInterest(repaymentDataModel.getRepayment_amount(), repaymentDataModel.getEstimation_repayment_time(),currTime);
            ContactInfo contactInfo = StringUtil.getObject(userDataModel.getContacts(),ContactInfo.class);
            Map<String,Object> result = new HashMap<String,Object>();
            result.put("name",userDataModel.getId_name());
            result.put("order_no",orderModel.getOrder_no());
            result.put("mobile_number",userModel.getMobile_number());
            result.put("contact_id_name","");
            result.put("contact_relation","");
            result.put("contact_mobile_number","");
            if(null != contactInfo){
                result.put("contact_id_name",contactInfo.getIdName());
                result.put("contact_relation",contactInfo.getRelation());
                result.put("contact_mobile_number",contactInfo.getMobile_number());
            }
            result.put("repayment_date",DateUtil.timestampToDateString(repaymentDataModel.getEstimation_repayment_time(), DateUtil.PATTERN_yyyy_MM_dd));
            result.put("prcie",NumberUtil.formatNumber(repaymentDataModel.getRepayment_amount(),NumberUtil.FORMAT_4));
            result.put("overdue_price",NumberUtil.formatNumber(overdueDto.getOverduePrice(),NumberUtil.FORMAT_4));
            result.put("overdue_day",overdueDto.getOverdueDay());
            result.put("sum_price",NumberUtil.formatNumber(ArithUtil.add(overdueDto.getOverduePrice(),repaymentDataModel.getRepayment_amount()),NumberUtil.FORMAT_4));
            this.successData(FrameUtil.newHashMap("overdueInfo",result));
        }

    }
